package com.testdemo;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.PersistentCookieStore;

import java.util.LinkedList;
import java.util.List;

import okhttp3.OkHttpClient;


/**
 * Created by cy on 2017/3/18.
 */

public class MyApplication extends Application {
    public static MyApplication myApplication;
    public List<Activity> mActivities = new LinkedList();


    @Override
    public void onCreate() {
        super.onCreate();

        myApplication = this;
        Fresco.initialize(this);

        OkGo.init(this);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.cookieJar(new CookieJarImpl(new PersistentCookieStore()));
        OkGo.getInstance()
//                .addInterceptor(new LoggerInterceptor("OkGoDebug", BuildConfig.DEBUG)) //建议设置OkHttpClient，不设置会使用默认的
                .setConnectTimeout(10 * 1000)               //全局的连接超时时间
                .setReadTimeOut(60 * 1000)                  //全局的读取超时时间
                .setWriteTimeOut(60 * 1000)
                .setCacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)               //全局统一缓存模式，默认不使用缓存，可以不传
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                //可以全局统一设置超时重连次数,默认为三次,那么最差的情况会请求4次(一次原始请求,三次重连请求),不需要可以设置为0
                .setRetryCount(0)
                .setCookieStore(new PersistentCookieStore());//MemoryCookieStore()
                //.addCommonHeaders(headers);

    }


    /***
     * 在每个Activity的onCreate中调用，用来记录打开了的activity
     */
    public void addActivity(Activity activity) {
        if (mActivities == null) {
            mActivities = new LinkedList<Activity>();
        }
        mActivities.add(activity);
    }

    /**
     * 集合中移除指定Activity
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        if (activity != null) {
            mActivities.remove(activity);
        }
    }

    /**
     * 关闭指定Activity
     *
     * @param activityClass 传入要finish的Activity类类型（例如:TestActivity.class）
     */
    public void closeActivity(Class activityClass) {
        if (mActivities != null && mActivities.size() > 0) {
            for (int i = 0; i < mActivities.size(); i++) {
                if (mActivities.get(i).getClass() == activityClass) {
                    mActivities.get(i).finish();
                }
            }
        }
    }

    public static MyApplication getInstance() {
        if (myApplication == null) {
            myApplication = new MyApplication();
        }
        return myApplication;
    }

    /**
     * 获得指定Activity
     *
     * @param activityClass 传入要获得的Activity类类型（例如:TestActivity.class）
     */
    public Activity getActivity(Class activityClass) {
        if (mActivities != null && mActivities.size() > 0) {
            for (int i = 0; i < mActivities.size(); i++) {
                if (mActivities.get(i).getClass() == activityClass) {
                    return mActivities.get(i);
                }
            }
        }
        return null;
    }

    /**
     * 获得最外层的Activity
     */
    public Activity getLastActivity() {
        if (mActivities != null && mActivities.size() > 0) {
            return mActivities.get(mActivities.size() - 1);
        }
        return null;
    }

    /***
     * 结束所有的activity
     */
    public void finishAll() {
        try {
            for (Activity activity : mActivities) {
                if (activity != null) {
                    activity.finish();
                }
            }
        } catch (Exception e) {
        }

    }


    //由于65K限制，采用分包（Dex）
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
